package com.example.house

import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.house.adapter.HouseApplyAdapter
import com.example.house.databinding.ActivityHouseApplyBinding
import com.example.house.vm.HouseApplyViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/house/HouseApplyActivity")
@AndroidEntryPoint
class HouseApplyActivity : BaseMvvmActivity<ActivityHouseApplyBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_house_apply
    private val houseApplyViewModel : HouseApplyViewModel by viewModels()
    val houseApplyAdapter = HouseApplyAdapter()
    override fun initView() {
        super.initView()
        mDataBinding.rv.adapter = houseApplyAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)
        houseApplyViewModel.mSuccessLiveData.observe(this){
            houseApplyAdapter.data.clear()
            houseApplyAdapter.data.addAll(it.data)
            houseApplyAdapter.notifyDataSetChanged()
        }
        houseApplyViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
                mDataBinding.back.setOnClickListener {
                    finish()
                }
        houseApplyAdapter.setOnItemClickListener { adapter, view, position ->
            val houseAddEntity = houseApplyAdapter.data[position]
            ARouter.getInstance().build( "/house/HouseListActivity")
                .withParcelable("houseAddEntity",houseAddEntity)
                .navigation()
        }
        mDataBinding.no.setOnClickListener {
            setSelectedButton(mDataBinding.no, mDataBinding.yes)
        }
        mDataBinding.yes.setOnClickListener {
            setSelectedButton(mDataBinding.yes, mDataBinding.no)
        }
    }

    override fun onResume() {
        super.onResume()
        houseApplyViewModel.getHouseApply()
    }

    private fun setSelectedButton(selectedButton: TextView, unselectedButton: TextView) {
        // 设置选中状态
        selectedButton.setBackgroundResource(R.drawable.selected_background) // 选中背景
        selectedButton.setTextColor(getColor(R.color.white)) // 选中文字颜色

        // 设置未选中状态
        unselectedButton.setBackgroundResource(R.drawable.normal_background) // 正常背景
        unselectedButton.setTextColor(getColor(com.example.resource.R.color.grey)) // 正常文字颜色
    }
}